package exec

import (
	"encoding/xml"
)

var Mediainfo = &mediainfo{}

type mediainfo struct {
	//env
}

type MediaInfoResult struct {
	Tracks  []track       `json:"tracks,omitempty"`
	General *trackGeneral `json:"general,omitempty"`
	Video   *trackVideo   `json:"video,omitempty"`
	Audio   *trackAudio   `json:"audio,omitempty"`
	Image   *trackImage   `json:"image,omitempty"`
}

type mi struct {
	XMLName xml.Name `xml:"MediaInfo"`
	Media   media    `xml:"media"`
}

type track struct {
	Type                           string  `xml:"type,attr"`
	Ref                            string  `xml:"ref,attr"`
	VideoCount                     int     `xml:"VideoCount"`
	AudioCount                     int     `xml:"AudioCount"`
	FileExtension                  string  `xml:"FileExtension"`
	Format                         string  `xml:"Format"`
	FormatProfile                  string  `xml:"Format_Profile"`
	CodecID                        string  `xml:"CodecID"`
	CodecIDCompatible              string  `xml:"CodecID_Compatible"`
	FileSize                       int64   `xml:"FileSize"`
	Duration                       float64 `xml:"Duration"`
	OverallBitRate                 int64   `xml:"OverallBitRate"`
	FrameRate                      float64 `xml:"FrameRate"`
	FrameCount                     int     `xml:"FrameCount"`
	StreamSize                     int64   `xml:"StreamSize"`
	HeaderSize                     int64   `xml:"HeaderSize"`
	DataSize                       int64   `xml:"DataSize"`
	FooterSize                     int64   `xml:"FooterSize"`
	IsStreamable                   string  `xml:"IsStreamable"`
	FileModifiedDate               string  `xml:"File_Modified_Date"`
	FileModifiedDateLocal          string  `xml:"File_Modified_Date_Local"`
	EncodedApplication             string  `xml:"Encoded_Application"`
	Width                          int     `xml:"Width"`
	Height                         int     `xml:"Height"`
	ColorSpace                     string  `xml:"ColorSpace"`
	ChromaSubsampling              string  `xml:"ChromaSubsampling"`
	BitDepth                       int     `xml:"BitDepth"`
	CompressionMode                string  `xml:"Compression_Mode"`
	StreamOrder                    string  `xml:"StreamOrder"`
	ID                             int     `xml:"ID"`
	FormatLevel                    string  `xml:"Format_Level"`
	FormatSettingsCABAC            string  `xml:"Format_Settings_CABAC"`
	FormatSettingsRefFrames        string  `xml:"Format_Settings_RefFrames"`
	BitRate                        float64 `xml:"BitRate"`
	SampledWidth                   int     `xml:"Sampled_Width"`
	SampledHeight                  int     `xml:"Sampled_Height"`
	PixelAspectRatio               float64 `xml:"PixelAspectRatio"`
	DisplayAspectRatio             float64 `xml:"DisplayAspectRatio"`
	Rotation                       float64 `xml:"Rotation"`
	FrameRateMode                  string  `xml:"FrameRate_Mode"`
	FrameRateModeOriginal          string  `xml:"FrameRate_Mode_Original"`
	ScanType                       string  `xml:"ScanType"`
	ColourDescriptionPresent       string  `xml:"colour_description_present"`
	ColourDescriptionPresentSource string  `xml:"colour_description_present_Source"`
	ColourRange                    string  `xml:"colour_range"`
	ColourRangeSource              string  `xml:"colour_range_Source"`
	ColourPrimaries                string  `xml:"colour_primaries"`
	ColourPrimariesSource          string  `xml:"colour_primaries_Source"`
	TransferCharacteristicsSource  string  `xml:"transfer_characteristics_Source"`
	MatrixCoefficientsSource       string  `xml:"matrix_coefficients_Source"`
	FormatAdditionalFeatures       string  `xml:"Format_AdditionalFeatures"`
	BitRateMode                    string  `xml:"BitRate_Mode"`
	Channels                       int     `xml:"Channels"`
	ChannelPositions               string  `xml:"ChannelPositions"`
	ChannelLayout                  string  `xml:"ChannelLayout"`
	SamplesPerFrame                int     `xml:"SamplesPerFrame"`
	SamplingRate                   int64   `xml:"SamplingRate"`
	SamplingCount                  int64   `xml:"SamplingCount"`
	StreamSizeProportion           float64 `xml:"StreamSize_Proportion"`
	Default                        string  `xml:"Default"`
	AlternateGroup                 string  `xml:"AlternateGroup"`
}

type media struct {
	Tracks []track `xml:"track"`
}

type trackGeneral struct {
	Type           string  `xml:"type,attr"`
	VideoCount     int     `xml:"VideoCount"`
	AudioCount     int     `xml:"AudioCount"`
	FileExtension  string  `xml:"FileExtension"`
	Format         string  `xml:"Format"`
	CodecID        string  `xml:"CodecID"`
	FileSize       int64   `xml:"FileSize"`
	Duration       float64 `xml:"Duration"`
	OverallBitRate int64   `xml:"OverallBitRate"`
	FrameRate      float64 `xml:"FrameRate"`
	FrameCount     int     `xml:"FrameCount"`
	StreamSize     int64   `xml:"StreamSize"`
}
type trackVideo struct {
	Type             string  `xml:"type,attr"`
	StreamOrder      string  `xml:"StreamOrder"`
	ID               int     `xml:"ID"`
	Format           string  `xml:"Format"`
	FormatProfile    string  `xml:"Format_Profile"`
	FormatLevel      string  `xml:"Format_Level"`
	CodecID          string  `xml:"CodecID"`
	Duration         float64 `xml:"Duration"`
	BitRate          float64 `xml:"BitRate"`
	Width            int     `xml:"Width"`
	Height           int     `xml:"Height"`
	PixelAspectRatio float64 `xml:"PixelAspectRatio"`
	Rotation         float64 `xml:"Rotation"`
	FrameRateMode    string  `xml:"FrameRate_Mode"`
	FrameRate        float64 `xml:"FrameRate"`
	FrameCount       int     `xml:"FrameCount"`
	StreamSize       int64   `xml:"StreamSize"`
}
type trackAudio struct {
	Type            string  `xml:"type,attr"`
	StreamOrder     string  `xml:"StreamOrder"`
	ID              int     `xml:"ID"`
	Format          string  `xml:"Format"`
	CodecID         string  `xml:"CodecID"`
	Duration        float64 `xml:"Duration"`
	BitRateMode     string  `xml:"BitRate_Mode"`
	BitRate         float64 `xml:"BitRate"`
	Channels        int     `xml:"Channels"`
	SamplingRate    int64   `xml:"SamplingRate"`
	FrameRate       float64 `xml:"FrameRate"`
	FrameCount      int     `xml:"FrameCount"`
	CompressionMode string  `xml:"Compression_Mode"`
	StreamSize      int64   `xml:"StreamSize"`
}
type trackImage struct {
	Type            string `xml:"type,attr"`
	Format          string `xml:"Format"`
	Width           int    `xml:"Width"`
	Height          int    `xml:"Height"`
	ColorSpace      string `xml:"ColorSpace"`
	BitDepth        int    `xml:"BitDepth"`
	CompressionMode string `xml:"Compression_Mode"`
	StreamSize      int64  `xml:"StreamSize"`
}

//func (r *mediainfo) GetMediaInfo(filepath string) (*MediaInfoResult, error) {
//	mi := &mi{}
//	general := &trackGeneral{}
//	video := &trackVideo{}
//	audio := &trackAudio{}
//	image := &trackImage{}
//
//	var tracks []track
//	out, err := exec.Command(r.envPath, "--Output=XML", "-f", filepath).Output()
//	if err != nil {
//		return nil, fmt.Errorf("[err]=%s, [out]=%s", err.Error(), string(out))
//	}
//
//	if err := xml.Unmarshal(out, &mi); err != nil {
//		return nil, err
//	}
//
//	for _, v := range mi.Media.Tracks {
//		tracks = append(tracks, v)
//		if v.Type == "General" {
//			general.Type = v.Type
//			general.VideoCount = v.VideoCount
//			general.AudioCount = v.AudioCount
//			general.FileExtension = v.FileExtension
//			general.Format = v.Format
//			general.CodecID = v.CodecID
//			general.FileSize = v.FileSize
//			general.Duration = v.Duration
//			general.OverallBitRate = v.OverallBitRate
//			general.FrameRate = v.FrameRate
//			general.FrameCount = v.FrameCount
//			general.StreamSize = v.StreamSize
//		} else if v.Type == "Video" {
//			video.Type = v.Type
//			video.ID = v.ID
//			video.Format = v.Format
//			video.FormatProfile = v.FormatProfile
//			video.FormatLevel = v.FormatLevel
//			video.CodecID = v.CodecID
//			video.Duration = v.Duration
//			video.BitRate = v.BitRate
//			video.Width = v.Width
//			video.Height = v.Height
//			video.PixelAspectRatio = v.PixelAspectRatio
//			video.Rotation = v.Rotation
//			video.FrameRateMode = v.FrameRateMode
//			video.FrameRate = v.FrameRate
//			video.FrameCount = v.FrameCount
//			video.StreamSize = v.StreamSize
//		} else if v.Type == "Audio" {
//			audio.Type = v.Type
//			audio.ID = v.ID
//			audio.Format = v.Format
//			audio.CodecID = v.CodecID
//			audio.Duration = v.Duration
//			audio.BitRateMode = v.BitRateMode
//			audio.BitRate = v.BitRate
//			audio.Channels = v.Channels
//			audio.SamplingRate = v.SamplingRate
//			audio.FrameRate = v.FrameRate
//			audio.FrameCount = v.FrameCount
//			audio.CompressionMode = v.CompressionMode
//			audio.StreamSize = v.StreamSize
//		} else if v.Type == "Image" {
//			image.Type = v.Type
//			image.Format = v.Format
//			image.Width = v.Width
//			image.Height = v.Height
//			image.ColorSpace = v.ColorSpace
//			image.BitDepth = v.BitDepth
//			image.CompressionMode = v.CompressionMode
//			image.StreamSize = v.StreamSize
//		}
//	}
//
//	return &MediaInfoResult{
//		General: general,
//		Video:   video,
//		Audio:   audio,
//		Image:   image,
//		Tracks:  tracks,
//	}, nil
//}
